package com.youku.component.result;

import java.io.IOException;

import org.codehaus.jackson.JsonNode;

import com.youku.exception.IllegalJsonException;
import com.youku.util.JsonHelper;

/**
 * 当响应结果格式为json时的处理方式
 * @author loda
 *
 */
public class JsonResponseBodyHandler implements ResponseBodyHandler {

	private JsonNode root;

	@Override
	public String handle(String field) {
		return JsonHelper.path(root, field);
	}

	@Override
	public void init(String responseBody) {
		try {
			root = JsonHelper.parse(responseBody);
		} catch (IOException e) {
			throw new IllegalJsonException("json格式不合法", e);
		}
	}

}
